/*
 * Copyright IBM Corp. and others 2001
 *
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which accompanies this
 * distribution and is available at https://www.eclipse.org/legal/epl-2.0/
 * or the Apache License, Version 2.0 which accompanies this distribution and
 * is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * This Source Code may also be made available under the following
 * Secondary Licenses when the conditions for such availability set
 * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
 * General Public License, version 2 with the GNU Classpath
 * Exception [1] and GNU General Public License, version 2 with the
 * OpenJDK Assembly Exception [2].
 *
 * [1] https://www.gnu.org/software/classpath/license.html
 * [2] https://openjdk.org/legal/assembly-exception.html
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
 */
package com.ibm.j9ddr.tools.ddrinteractive;

/**
 * Utility class for converting a value to a human-readable form.
 */
public class Humanize {

	/**
	 * Convert the byte value into something easier to understand.
	 * @param bytes
	 * @return
	 */
	public static String bytes(long bytes) {
		if (bytes < 1024)
			return bytes + " B";
		int exponent = (int) (Math.log(bytes) / Math.log(1024));
		char prefix = "KMGTPE".charAt(exponent - 1);
		return String.format("%.1f %sB", bytes / Math.pow(1024, exponent), prefix);
	}

	/**
	 * Make the long value look like a pointer.
	 * @param value
	 * @return
	 */
	public static String pointer(long value) {
		return "0x" + Long.toHexString(value).toUpperCase();
	}

	public static void main(String[] args) {
		System.out.println( Humanize.bytes(0) );
		System.out.println( Humanize.bytes(1023) );
		System.out.println( Humanize.bytes(1026) );
		System.out.println( Humanize.bytes(1024 * 1024 - 1) );
		System.out.println( Humanize.bytes(1024 * 1024) );
		System.out.println( Humanize.bytes(1024 * 1024 * 1023) );
		System.out.println( Humanize.bytes(1024 * 1024 * 1024) );
	}


}
